package com.qdev.apps.core.webwork.validator;

import java.util.Map;

import com.opensymphony.webwork.validators.ScriptValidationAware;

/**
 * Validates a string field using a regular expression.
 * 
 * @author Quake Wang
 * @since 2004-7-23
 * @version $Revision: 1.1 $
 */
public class JavaScriptRegexpFieldValidator extends RegexpFieldValidator implements ScriptValidationAware {
    public String validationScript(Map parameters) {
        String field = (String) parameters.get("name");
        StringBuffer js = new StringBuffer();
        
        js.append("value = form.elements['" + field + "'].value;\n");
        js.append("if (value != \"\" && !value.match(/" + getExpression() + "/)) {\n");
        js.append("\talert('" + getMessage(null) + "');\n");
        js.append("\treturn '" + field + "';\n");
        js.append("}\n");
        js.append("\n");

        return js.toString();
    }

}